import UIKit
import Flutter
import Photos
import MediaPlayer

///基本消息通道
///isOpen 是否暂停
///音乐播放器
/// 计时器
/// 计时器任务
/// 索引值
/// 音乐数组
/// 音乐原始数组
/// 音乐模型

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
     print("Received playMusic method call=====0")
      
      let artists = MPMediaQuery.artists()
      let artists1 = artists.items
      print("artists1: \(artists1)")
      
      let audiobooks = MPMediaQuery.audiobooks()
      let audiobooks1 = audiobooks.items
      print("audiobooks1: \(audiobooks1)")
      
      let playlists = MPMediaQuery.playlists()
      let playlists1 = playlists.items
      print("playlists1: \(playlists1)")
      
      let songs = MPMediaQuery.songs()
      let songs1 = songs.items
      print("songs1: \(songs1)")
      
     let options = PHFetchOptions()
     print("Received playMusic method call=====1")
     options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
     print("Received playMusic method call=====2")
      let fetchResult = PHAsset.fetchAssets(with: .video, options: options)
     print("Received playMusic method call=====3")
     fetchResult.enumerateObjects {
     (asset, _, _) in
     print("Received playMusic method call=====4")
     let resources = PHAssetResource.assetResources(for: asset)
     print("Resources count: \(resources.count)")
      for resource in resources {
          if resource.type == .photo {
              print("Received playMusic method call=====photo")
            }
          if resource.type == .video {
              print("Received playMusic method call=====video\(resource)")
            }
          if resource.type == .audio {
              print("Received playMusic method call=====audio\(resource)")
            }
          }
      }
      print("Received playMusic method call=====6")
      GeneratedPluginRegistrant.register(with: self)
      print("Received playMusic method call=====7")
      let messenger : FlutterBinaryMessenger = window?.rootViewController as! FlutterBinaryMessenger
      print("Received playMusic method call=====8")
      testPlugin(messenger: messenger)
      print("Received playMusic method call=====9")
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }



    
    func testPlugin(messenger: FlutterBinaryMessenger) {
    //"plugin_apple" 这是通道名称，需要和Flutter端定义的相同
    let channel = FlutterMethodChannel(name: "methodChannel", binaryMessenger: messenger)

    channel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
    if (call.method == "initMusics") {
        print("Received initMusics method call")
        result(["result": "success", "code": 200])
    }
    if (call.method == "playMusic") {
        print("Received playMusic method call")
        result(["result": "success", "code": 201])
    }
    if (call.method == "startMusic") {
        print("Received startMusic method call")
        result(["result": "success", "code": 202])
    }
    if (call.method == "stopMusic") {
        print("Received stopMusic method call")
        result(["result": "success", "code": 203])
    }
    if (call.method == "lastClick") {
        print("Received lastClick method call")
        result(["result": "success", "code": 204])
    }
    if (call.method == "nextClick") {
        print("Received nextClick method call")
        result(["result": "success", "code": 205])
    }
    if (call.method == "getFlutterInfo") {
        print("Received getFlutterInfo method call")
        result(["result": "success", "code": 206])
    }
}}

}


// class Bean {
//     var musicFileUrl: String
//     var musicTitle: String
//     var musicUrl: String
//     var musicId: String
//     var musicDuration: String
//     var musicFileName: String
//     var musicAuthor: String

//     init(musicFileUrl: String, musicTitle: String, musicUrl: String, musicId: String, musicDuration: String, musicFileName: String, musicAuthor: String) {
//         self.musicFileUrl = musicFileUrl
//         self.musicTitle = musicTitle
//         self.musicUrl = musicUrl
//         self.musicId = musicId
//         self.musicDuration = musicDuration
//         self.musicFileName = musicFileName
//         self.musicAuthor = musicAuthor
//     }

//     static func ==(lhs: Bean, rhs: Bean) -> Bool {
//         return lhs.musicFileUrl == rhs.musicFileUrl &&
//             lhs.musicTitle == rhs.musicTitle &&
//             lhs.musicUrl == rhs.musicUrl &&
//             lhs.musicId == rhs.musicId &&
//             lhs.musicDuration == rhs.musicDuration &&
//             lhs.musicFileName == rhs.musicFileName &&
//             lhs.musicAuthor == rhs.musicAuthor
//     }

//     func hash(into hasher: inout Hasher) {
//         hasher.combine(musicFileUrl)
//         hasher.combine(musicTitle)
//         hasher.combine(musicUrl)
//         hasher.combine(musicId)
//         hasher.combine(musicDuration)
//         hasher.combine(musicFileName)
//         hasher.combine(musicAuthor)
//     }
// }
